/*                                  ATELIER                                   */

/* %%LICENSE_TAG%%                                                            */
package edu.gmu.view.cube;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * The ControlPanel sets the various parameters for a 3D-coordinate system.
 * @author  James H. Pope
 * @version $Revision:$ $Date:$
 */
public class PanelScaling extends JPanel
{
    // The object that is interested in the values we produce
    private ControlModel controlModel = null;

    //-----------------------  Scaling Controls  -----------------------------//
    /**
     * xScaling
     */
    private JLabel xScalingLabel    = null;
    private JSpinner xScalingField  = null;

    /**
     * yScaling
     */
    private JLabel yScalingLabel    = null;
    private JSpinner yScalingField  = null;

    /**
     * zScaling
     */
    private JLabel zScalingLabel    = null;
    private JSpinner zScalingField  = null;

    /**
     * Creates a new instance of ControlPanel with specified values.
     * @param controlModel
     */
    public PanelScaling( ControlModel pControlModel )
    {
        this.controlModel = pControlModel;
        
        //--------------------  Scaling Controls  ----------------------------//
        double maxScale = 2.0;
        double minScale =-2.0;
        double stepScale = 0.1;
        this.xScalingLabel  = new JLabel( "X: " );
        final SpinnerNumberModel xScalingModel = new SpinnerNumberModel(
                controlModel.getXScaling(),
                minScale,
                maxScale,
                stepScale);
        xScalingModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = xScalingModel.getNumber().doubleValue();
                    controlModel.setXScaling( newValue );
                }
            }
        );
        this.xScalingField  = new JSpinner( xScalingModel );

        this.yScalingLabel  = new JLabel( "Y" );
        final SpinnerNumberModel yScalingModel = new SpinnerNumberModel(
                controlModel.getYScaling(),
                minScale,
                maxScale,
                stepScale);
        yScalingModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = yScalingModel.getNumber().doubleValue();
                    controlModel.setYScaling( newValue );
                }
            }
        );
        this.yScalingField  = new JSpinner( yScalingModel );

        this.zScalingLabel  = new JLabel( "Z" );
        final SpinnerNumberModel zScalingModel = new SpinnerNumberModel(
                controlModel.getZScaling(),
                minScale,
                maxScale,
                stepScale);
        zScalingModel.addChangeListener(new ChangeListener()
            {
                public void stateChanged(ChangeEvent ce)
                {
                    double newValue = zScalingModel.getNumber().doubleValue();
                    controlModel.setZScaling( newValue );
                }
            }
        );
        this.zScalingField  = new JSpinner( zScalingModel );

        // Add the labels/fields to the panel
        JPanel scalingPanel = new JPanel();
        scalingPanel.setBorder(BorderFactory.createTitledBorder( "Scaling" ));
        BoxLayout scalingPanelLayout = new BoxLayout( scalingPanel, BoxLayout.Y_AXIS );
        scalingPanel.setLayout( scalingPanelLayout );

        scalingPanel.add( createRow( xScalingLabel,    xScalingField ) );
        scalingPanel.add( createRow( yScalingLabel,    yScalingField ) );
        scalingPanel.add( createRow( zScalingLabel,    zScalingField ) );


        this.setLayout( new BorderLayout() );
        this.add( scalingPanel, BorderLayout.CENTER );
    }

    /**
     *
     * @param label
     * @param field
     * @return
     */
    public JPanel createRow( JLabel label, JComponent field )
    {
    	JPanel rowPanel = new JPanel( );
    	Dimension labelDim = new Dimension( 100, 20 );
    	Dimension fieldDim = new Dimension( 150, 20 );

    	label.setPreferredSize( labelDim );
    	field.setPreferredSize( fieldDim );

    	rowPanel.add( label );
    	rowPanel.add( field );

    	return rowPanel;
    }

}

/*                                  ATELIER                                   */